package com.ls.knowledge.llm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ls.knowledge.llm.dto.ChatMessageDto;
import com.ls.knowledge.llm.entity.ChatConversation;
import com.ls.knowledge.llm.entity.ChatConversationMessage;
import com.ls.knowledge.llm.service.ChatConversationMessageService;
import com.ls.knowledge.llm.service.ChatConversationService;
import com.ls.knowledge.llm.service.ChatService;
import com.ls.knowledge.llm.vo.ChatConversationVo;
import com.ls.knowledge.llm.vo.MessageVo;
import com.ls.knowledge.vo.R;
import com.ls.knowledge.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/llm/chat")
public class ChatController {

    @Resource
    private ChatService chatService;


    @Autowired
    private ChatConversationService chatConversationService;

    @Autowired
    private ChatConversationMessageService chatConversationMessageService;

    @GetMapping("/chatStream")
    public Flux<MessageVo> chatStream(@RequestParam("sseData") String sseData) {
        ChatMessageDto messageDto = JSONObject.parseObject(sseData, ChatMessageDto.class);
        return chatService.chatStream(messageDto);
    }


    @PostMapping("/chat")
    public String chat(@RequestBody ChatMessageDto dto) {
        return chatService.chat(dto);
    }


    @PostMapping("/conversationList")
    public Result<List<ChatConversation>> conversationList() {
        LambdaQueryWrapper<ChatConversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ChatConversation::getCreateTime);
        return R.ok(chatConversationService.list(queryWrapper));
    }


    @GetMapping("/getChatConversation")
    public Result<ChatConversationVo> getChatConversation(@RequestParam("conversationId") String conversationId) {
        ChatConversation conversation = chatConversationService.getById(conversationId);
        Assert.notNull(conversation, "对话不存在");
        List<ChatConversationMessage> messages = chatConversationMessageService.getConversationMessage(conversationId);

        ChatConversationVo conversationVo = BeanUtil.copyProperties(conversation, ChatConversationVo.class);
        conversationVo.setMessages(BeanUtil.copyToList(messages, MessageVo.class));
        return R.ok(conversationVo);
    }


    /**
     * 上传指定文件到对话中
     * @param file
     * @param conversationId
     * @return
     */
    @PostMapping("/uploadFileToConversation")
    public Result<Void> uploadFileToConversation(@RequestParam("file") MultipartFile file,@RequestParam("conversationId") String conversationId) {
        if (file.isEmpty()) {
            return R.fail("文件为空");
        }
        chatService.uploadFileToConversation(file,conversationId);
        return R.ok();
    }





}
